package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public PageResponseResult findByQuery(ChannelDTO dto) {
        //1.阐述校验代码
        dto.checkParam();
        //2.分页查询
        //2.1封装分页查询条件
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2其他查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) { //true 代表name不为空
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {  //如果状态不为null，使用状态查询
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        //2.3按照ord升序排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //2.3执行查询操作
        //参数1：分页条件 参数2：查询条件
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //3.封装成pageResponseResult结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.校验参数
        String name = adChannel.getName();
        //1.1名字不能为空
        /*if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名字不能为空");
        }*/
        //1.2名字长度不能大于10
        /*if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "2名字长度不能大于10");
        }*/
        //1.3校验频道名称不能重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已经存在");
        }
        //2.将频道保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3.返回成功的响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        Integer adChannelid = adChannel.getId();
        //1.1检查频道id
        if (adChannelid == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        //1.2根据id查询频道信息
        AdChannel oldChannel = getById(adChannelid);
        if (oldChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道不存在");
        }
        //1.3判断参数name是否为空，如果频道名称不为空，判断和原来的频道名称是否一致
        String name = adChannel.getName();
        //频道名称不为空，并且频道名称做了改变，需要校验一下频道名称
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel.getName())) {
            //1.2名字长度不能大于10
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "2名字长度不能大于10");
            }
            //1.3校验频道名称不能重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已经存在");
            }
        }
        //2.修改频道
        this.updateById(adChannel);
        //3.响应结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
            //1.校验参数
            AdChannel adChannel = getById(id);
            if(adChannel==null){
                ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不存在");
            }
            //根据id查询频道
            Boolean status = adChannel.getStatus();
            //判断频道是否启用
            if(status){//启用，频道有效不能删除
                ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效，无法删除");
            }
            //2.如果未启用 删除频道
            this.removeById(id);

            if(id==50){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
            }
        //3.返回响应结果
        return ResponseResult.okResult();
    }
}
